package com.yl.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yl.auth.entity.User;
import com.yl.auth.entity.Users;
import com.yl.auth.mapper.UsersMapper;
import com.yl.auth.mapper.YuangongMapper;
import com.yl.auth.model.entity.Yuangong;
import com.yl.auth.model.vo.UserVo;
import com.yl.auth.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yl.auth.utils.TokenUtils;
import com.yl.common.execption.GuiguException;
import com.yl.common.result.ResultCodeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author W
 * @since 2023-07-24
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private YuangongMapper yuangongMapper;
    private static final Log LOG = Log.get();

    @Override
    public Users login(Users users) {
        Users userInfo = getUserInfo(users);
        if(userInfo!=null) {
            BeanUtil.copyProperties(userInfo,users,true);
            //设置token
            String token = TokenUtils.getToken(userInfo.getId().toString(), userInfo.getPassword());
            users.setToken(token);
            return users;
        }else {
            throw new GuiguException(ResultCodeEnum.SERVICE_ERROR);
        }
    }

    @Override
    public Users getPrimId(Integer id) {
       return usersMapper.getPrimId(id);
    }


    @Override
    public void saveYuangong(Yuangong yuangong) {
        Users users=new Users();
        users.setUsername(yuangong.getAccount());
        users.setPassword(yuangong.getPassword());
        users.setImgurl(yuangong.getAvatar());
        users.setName(yuangong.getName());
        users.setIdcard(yuangong.getIdcard());
        users.setPhone(yuangong.getPhone());
        users.setType("3");
        usersMapper.insert(users);
        yuangong.setUsersId(users.getId());
        yuangongMapper.insert(yuangong);
    }


    @Override
    public Users getZxPrimId(Integer id) {
        return usersMapper.getZxPrimId(id);
    }


    private Users getUserInfo(Users user){
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("password",user.getPassword());
        Users one;
        try{
            one = getOne(wrapper);
        }catch (Exception e) {
            LOG.error(e);
            throw new GuiguException(ResultCodeEnum.CODE_600);
        }
        return one;
    }
    @Override
    public Integer getMax() {
        return usersMapper.getMax();
    }

    @Override
    public Users getJiGouDuan(Integer id) {
        return usersMapper.getJiGouDuan(id);
    }

    @Override
    public Users getYuanGong(Integer id) {
        return usersMapper.getYuanGong(id);
    }

    @Override
    public void removeRole(Yuangong yuangong) {
        usersMapper.reomveRole(yuangong);
    }
}
